package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAmtsVo;
import com.qf.vo.UserOrderCntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin("http://localhost")
public class DashboardController {
   // private static final Logger logger= LoggerFactory.getLogger(DashboardController.class);
    @Autowired
    UserService userService;
    @Autowired
    GoodService goodService;
    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;
    @Autowired
    DtsCategoryService categoryService;

    @GetMapping("/chart")
    public Object chart(){
        List<CategorySellAmts>categorySellAmts=categoryService.getCategorySellAmts();
        List<String>categoryNames=fetchCategoryNames(categorySellAmts);
        Map<String,Object> map=new HashMap<>();
        map.put("categoryNames",categoryNames);
        map.put("categorySellData",categorySellAmts);
        //统计最近一个月范围内的订单的总额和订单笔数（包含一个月没的日期）
        List<DayStatis>dayStatis=orderService.findDayStatis(30);
        //提取出dayStatis中的日期字符串
        String[] dayData=fetchDayData(dayStatis);
        OrderAmtsVo orderAmtsVo=new OrderAmtsVo();
        orderAmtsVo.setDayData(dayData);
        //提取出dayStatis中的订单笔数
        int[] cnt=fetchCnt(dayStatis,dayData);
        orderAmtsVo.setOrderCntData(cnt);
        //提取出dayStatis中的订单总金额
        BigDecimal[] orderAmtData=fetchAmts(dayStatis,dayData);
        orderAmtsVo.setOrderAmtData(orderAmtData);
        Map<String,Object>data=new HashMap<>();
        data.put("categorySell",map);
        data.put("orderAmts", orderAmtsVo);

        //统计最近一个月范围内新增用户的情况
        List<DayStatis>userDayStatis =userService.findUserDayStatis(30);
        UserOrderCntVo userOrderCntVo=new UserOrderCntVo();
        //把user和order存储到一个数组中
        String[] userDayStr=unionDayStr(dayStatis,userDayStatis);
        int[] userCnt=fetchUserCnt(userDayStatis,userDayStr);
        //将日新增用户的天数添加到对象中
        userOrderCntVo.setDayData(userDayStr);

        //将新增订单的数量添加到对象中
        userOrderCntVo.setUserCnt(userCnt);

        int[] orderCnt=fetchOrderCnt(dayStatis,userDayStr);
         userOrderCntVo.setOrderCnt(orderCnt);
        //日期对应的订单数
        data.put("userOrderCnt",  userOrderCntVo);

        return ResponseUtil.ok(data);
    }

    private int[] fetchOrderCnt(List<DayStatis> dayStatis, String[] userDayStr) {
        int[] arr = new int[userDayStr.length];
        if(dayStatis != null && dayStatis.size() > 0){
            for (int i = 0; i < userDayStr.length; i++) {
                for (DayStatis dayStati : dayStatis) {
                    String dayStr = dayStati.getDayStr();
                    if(userDayStr[i].equalsIgnoreCase(dayStr)){
                        arr[i] = dayStati.getCnts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private String[] unionDayStr(List<DayStatis> dayStatis, List<DayStatis> userDayStatis) {
            Set<String>set=new HashSet<>();
        //把User和Order结果的日期都添加到set集合中
        if(dayStatis!=null && dayStatis.size()>0){
            for (DayStatis dayStati : dayStatis) {
                set.add(dayStati.getDayStr());
            }
        }
        if(userDayStatis!=null && userDayStatis.size()>0){
            for (DayStatis dayStati : userDayStatis) {
                set.add(dayStati.getDayStr());
            }
        }
        ArrayList<String> strings = new ArrayList<>(set);
        Collections.sort(strings);
        String[] strings1 = strings.toArray(new String[]{});
        return strings1;
    }

    private int[] fetchUserCnt(List<DayStatis> userDayStatis, String[] userDay) {
        int [] arr=new int[userDay.length];
        if(userDayStatis!=null&&userDayStatis.size()>0){
            for(int i=0;i<userDay.length;i++){
                for (DayStatis userDayStati : userDayStatis) {
                    String dayStr = userDayStati.getDayStr();
                    if(userDay[i].equalsIgnoreCase(dayStr)){
                        arr[i]=userDayStati.getCnts();
                    }
                }
            }
        }
        return arr;
    }


    private BigDecimal[] fetchAmts(List<DayStatis> dayStatis, String[] dayData) {
        BigDecimal[] arr=new BigDecimal[dayData.length];
        if(dayStatis!=null && dayStatis.size()>0){
            for(int i=0;i<dayData.length;i++){
                for (DayStatis dayStati : dayStatis) {
                    String dayStr = dayStati.getDayStr();
                    if(dayData[i].equalsIgnoreCase(dayStr)){
                        arr[i]=dayStati.getAmts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private int[] fetchCnt(List<DayStatis> dayStatis,String[] daData) {
        int[] arr=new int[daData.length];
        //提取数据中的订单数量，存储到集合中
        if(dayStatis!=null && dayStatis.size()>0){
            for(int i=0;i<daData.length;i++){
                for (DayStatis dayStati : dayStatis) {
                    String dayStr = dayStati.getDayStr();
                    if(daData[i].equalsIgnoreCase(dayStr)){
                        arr[i]=dayStati.getCnts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private String[] fetchDayData(List<DayStatis> orderDayStatis) {
        List<String>dayList=new ArrayList<>();
        //提取数据中的日期字符串，存储到数组中
        if(orderDayStatis!=null && orderDayStatis.size()>0){
            for (DayStatis dayStati : orderDayStatis) {
                //取出dayStati中的日期存放到dayList集合中
                String dayStr = dayStati.getDayStr();
                dayList.add(dayStr);

            }
        }
        //升序排序
        Collections.sort(dayList);
        //将集合转换为数组
        String[] dayArr=dayList.toArray(new String[]{});
        return dayArr;
    }

    private List<String> fetchCategoryNames(List<CategorySellAmts> CategorySellAmtslists){
        //创建返回值对象
        List<String> names=new ArrayList<>();
        if(CategorySellAmtslists!=null && CategorySellAmtslists.size()>0){

            for (CategorySellAmts categorySellAmtslist : CategorySellAmtslists) {
                //取出CategorySellAmts中所有的names，存储到names的集合中
                 names.add(categorySellAmtslist.getName());
            }
        }
        return names;
    }

    @GetMapping
    public Object info(){
        int userTotal= userService.showAllUserCount();
        int goodsTotal=goodService.showAllGoodCount();
        int orderTotal=orderService.showAllOrderCount();
        int productTotal=productService.showAllOrderCount();
        Map<String,Integer> date=new HashMap<>();
        date.put("userTotal",userTotal);
        date.put("goodsTotal",goodsTotal);
        date.put("productTotal",productTotal);
        date.put("orderTotal",orderTotal);
        return ResponseUtil.ok(date);
    }



}
